<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        li {
            list-style: none;
        }

        .banner {
            width: 300px;
            height: 200px;
            margin: 100px auto;
            background-color: #f00;
            overflow: hidden;
            position: relative;
        }



        .banner ul li {
            width: 300px;
            height: 200px;
            text-align: center;
            line-height: 200px;
            font-size: 50px;
            position: absolute;
            top: 0;
            left: 0;
            opacity: 0;
            transition: .3s;
        }


        .banner ol {
            width: 100%;
            position: absolute;
            display: flex;
            justify-content: center;
            bottom: 0;
        }

        .banner ol li {
            width: 20px;
            height: 20px;
            background-color: #ff0;
            margin: 10px;
        }

        .banner span {
            width: 20px;
            height: 30px;
            background-color: rgba(0, 0, 0, .5);
            position: absolute;
            display: flex;
            justify-content: center;
            align-items: center;
            top: 0;
            bottom: 0;
            margin: auto;
            color: #fff;
        }

        #next {
            right: 0;
        }

        .show {
            opacity: 1 !important;
        }

        .active {
            background-color: blueviolet !important;
        }
    </style>
</head>

<body>


    <div class="banner" id="a">
        <ul id="ul" class="u">
            <!-- <li class="show">1</li> -->

        </ul>
        <ol>
            <!-- <li class="dot active"></li> -->

        </ol>
        <p>
            <span id="prev">&lt;</span>
            <span id="next">&gt;</span>
        </p>
    </div>


    <div class="banner" id="b">
        <ul id="ul" class="u">
            <!-- <li class="show">1</li> -->

        </ul>
        <ol>
            <!-- <li class="dot active"></li> -->

        </ol>
        <p>
            <span id="prev">&lt;</span>
            <span id="next">&gt;</span>
        </p>
    </div>



    <script>

        // 图片的是动态生成的

        const imgArr = [1, 2, 3, 4];


        banner(imgArr , '#a') ;

        banner(imgArr , '#b') ;



        function banner(imgArr , seletor) {
            const oBanner = document.querySelector(seletor);
            const oUl = oBanner.querySelector('ul');
            const oOl = oBanner.querySelector('ol');
            // 翻页
            let index = 0;
            // 定时器
            let t = null;

            // 生成图片和小点
            renderList();   // 才有图片

            const oUlLis = oUl.querySelectorAll('li');
            const oOlLis = oOl.querySelectorAll('li');


            autoPlay();


            oBanner.onclick = function (e) {
                const ev = e || event;
                const target = ev.target || ev.srcElement;
                // 上翻页
                if (target.id === 'prev') {
                    clearInterval(t);
                    if (index === 0) {
                        index = oUlLis.length
                    }
                    index--;
                    show()
                    return
                }
                // 下翻页
                if (target.id === 'next') {
                    clearInterval(t);
                    if (index === oUlLis.length - 1) {
                        index = -1
                    }
                    index++;
                    show()
                    return
                }
                // 小点切换
                if (target.className === 'dot') {
                    clearInterval(t);
                    index = target.getAttribute('index');
                    show();
                }
            }





            // 自动播放
            function autoPlay() {
                t = setInterval(() => {
                    index++;
                    if (index === oUlLis.length) {
                        index = 0;
                    }
                    show()
                }, 1000)
            }


            function show() {
                // 清除所有，显示当前
                oUlLis.forEach((v, i) => {
                    v.classList.remove('show');
                    oOlLis[i].classList.remove('active');
                })
                oUlLis[index].classList.add('show');
                oOlLis[index].classList.add('active');
            }


            // 生成图片和小点
            function renderList() {
                let imgHTML = '';
                let dotHTML = '';
                imgArr.forEach((v, i) => {
                    // imgHTML += `<li><img src="${v}"></li>`
                    imgHTML += `<li>${v}</li>`
                    dotHTML += `<li class="dot" index=${i}></li>`
                })
                oUl.innerHTML = imgHTML;
                oOl.innerHTML = dotHTML;
                const oUlLis = oUl.querySelectorAll('li');
                const oOlLis = oOl.querySelectorAll('li');
                oUlLis[0].classList.add('show');
                oOlLis[0].classList.add('active');
            }



        }








    </script>

</body>

</html>